package main

import "fmt"

// 实现部分接口：渲染API
type Renderer interface {
	DrawCircle(radius float32)
	DrawRectangle(width, height float32)
}

// ---------------------- OpenGL实现 ----------------------
type OpenGLRenderer struct{}

func (r *OpenGLRenderer) DrawCircle(radius float32) {
	fmt.Printf("OpenGL绘制圆形(半径:%.1f)\n", radius)
}

func (r *OpenGLRenderer) DrawRectangle(width, height float32) {
	fmt.Printf("OpenGL绘制矩形(宽:%.1f 高:%.1f)\n", width, height)
}

// ---------------------- Vulkan实现 ----------------------
type VulkanRenderer struct{}

func (r *VulkanRenderer) DrawCircle(radius float32) {
	fmt.Printf("Vulkan绘制圆形(半径:%.1f)\n", radius)
}

func (r *VulkanRenderer) DrawRectangle(width, height float32) {
	fmt.Printf("Vulkan绘制矩形(宽:%.1f 高:%.1f)\n", width, height)
}

// 抽象部分：图形基类
type Shape struct {
	renderer Renderer // 桥接关键：持有实现接口
}

func NewShape(r Renderer) *Shape {
	return &Shape{renderer: r}
}

// ---------------------- 具体图形：圆形 ----------------------
type Circle struct {
	*Shape
	radius float32
}

func NewCircle(r Renderer, radius float32) *Circle {
	return &Circle{
		Shape:  NewShape(r),
		radius: radius,
	}
}

func (c *Circle) Draw() {
	c.renderer.DrawCircle(c.radius) // 委托给实现部分
}

// ---------------------- 具体图形：矩形 ----------------------
type Rectangle struct {
	*Shape
	width  float32
	height float32
}

func NewRectangle(r Renderer, w, h float32) *Rectangle {
	return &Rectangle{
		Shape:  NewShape(r),
		width:  w,
		height: h,
	}
}

func (r *Rectangle) Draw() {
	r.renderer.DrawRectangle(r.width, r.height) // 委托给实现部分
}

// ---------------------- 客户端调用 ----------------------
func main() {
	// 创建渲染器实现
	opengl := &OpenGLRenderer{}
	vulkan := &VulkanRenderer{}

	// 组合图形与渲染技术
	circle := NewCircle(vulkan, 5.0)
	rect := NewRectangle(opengl, 10, 20)

	circle.Draw() // 输出：Vulkan绘制圆形(半径:5.0)
	rect.Draw()   // 输出：OpenGL绘制矩形(宽:10.0 高:20.0)

	//// 扩展演示：新增Metal渲染器无需修改图形类
	//metal := &MetalRenderer{} // 假设已实现
	//triangle := NewTriangle(metal, 3,4,5)
	//triangle.Draw()
}
